define(["jquery","bbcare","fu/app/comm/validate","fu/app/comm/attention",
		"scprit/followup/app/question/question.ui.js",
		"scprit/followup/app/question/question.event.js",
		"scprit/followup/app/question/question.servers.js",
		"scprit/followup/app/patientManager/modifyPatientDiabetes.servers.js"],
	function ($,$bb,$validate,$attention,$questionUi,$questionEvent,$questionServers,$servers) {
		var event = {};
		var sessinonUserName = JSON.parse($bb.getSessionStorage("account")).alias;
		/*
		* 修改保存
		* */
		var questionsToSaveCallback = function(data,params){
			if(data.success){
				$bb.toastShow("保存成功",1500);
				$bb.openPage("motionAndBG",params);
			}else{
				$bb.toastShow("保存失败",1500);
			}
		};
		var modifySaveFn = function(){
			if($validate.init()){
				var params = $(this).data("params");
				var domId = $("#questionDetail form").attr("id");
				var questiones = $questionServers.getDataForDom(domId);
				var inParams = {
					oprInfo:{
						followQueueId: params.followQueueId,
						opCode:"1001",
						opNote:"【"+sessinonUserName + "】医生为【"+params.patientName+"】患者进行建档信息修改",
						opType:"2",
						patientId: params.patientId,
						ptSchemeId: params.patientSchemeId
					},
					questiones: questiones,
					task: {
						ptTaskId:params.ptTaskId,
						seqNo:params.seqNo,
						ptContentIds:params.ptContentIds
					}
				};
				var otherParams = {
					followQueueId: params.followQueueId,
					patientId: params.patientId,
					patientSchemeId: params.patientSchemeId,
					queuename:params.queuename
				};
				$servers.questionsToSave(inParams,questionsToSaveCallback,otherParams);
			}
		};
		/*
		* 获取建档问卷的回调
		* */
		var getRecordQuestionsCallback = function(data,params){
			params.ptContentIds = data.data.task.ptContentIds;
			params.patientName = data.data.patientInfo.userName;
			$('.panel-heading').html(params.queuename);
			$questionUi.init({
				id: "questionDetail",
				list: data.data.questiones,
				showIcon: true,
				values: data.data.values
			});
		}
		/*
		* 获取任务id列表的回调
		* */
		var getTaskIdCallback = function(data,params){
			var rows = data.data.rows,taskId,taskName,seqNo;
			for(var i = 0;i < rows.length;i++){
				if(rows[i].seqNo == 0){
					taskId = rows[i].taskId;
					seqNo = rows[i].seqNo;
					break;
				}
			}
			var params2 = $.extend({},params,{"ptTaskId":taskId,"seqNo":seqNo});
			var topbars = [
				{
					"icon":"fa fa-floppy-o",
					"name":"保存",
					"callback":modifySaveFn,
					"params":params2
				}
			];
			$bb.toolbar(topbars);
			var inParams = {"followQueueId":params.followQueueId,"ptTaskId":taskId};
			$servers.getRecordQuestions(inParams,getRecordQuestionsCallback,params2);

		};
		event.init = function(){
			var pageParams = $bb.getPageParams();
			var params = {followQueueId:pageParams.followQueueId,patientSchemeId:pageParams.patientSchemeId};
			$servers.getTaskId(params,getTaskIdCallback,pageParams);
		};
		return event;
});